package day327;

import common.TreeNode;

/**
 * @author tmh
 * @date 2023/3/27 22:36
 * @description
 */
public class T105从前序与中序遍历序列构造二叉树 {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null || root == p || root == q) return root;
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if(left == null) {
            return right;
        }
        if(right == null){
            return left;
        }
        return root;
    }
}
